package com.ponhu.ea2.goodsCenter.provider.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ponhu.ea2.entity.LuxPrice;
import com.ponhu.ea2.entity.LuxSkuInfo;
import com.ponhu.ea2.goodsCenter.provider.mapper.LuxPriceMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.LuxSkuInfoMapper;
import com.ponhu.ea2.goodsCenter.provider.mapper.PonHuGoodsTradePriceInfoMapper;
import com.ponhu.ea2.vo.PonHuGoodsTradePriceInfoVo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author luyu
 * @create 2021-09-16 16:15
 * 拉取胖总管商品数据的价格同步到商品中心的同行价中
 */
@Slf4j
@Component
public class TradePriceByPonhuForUpdateTask {

    @Resource
    private PonHuGoodsTradePriceInfoMapper ponHuGoodsTradePriceInfoMapper;

    @Resource
    private LuxPriceMapper luxPriceMapper;

    @Resource
    private LuxSkuInfoMapper luxSkuInfoMapper;

    @XxlJob("tradePriceByPonhuForUpdateTask")
    public void tradePriceByPonhuForUpdateTask(){
        // 查询三里屯门店所有的商品
        List<PonHuGoodsTradePriceInfoVo> list =
                ponHuGoodsTradePriceInfoMapper.queryPonhuGoodspriceInfo();
        Map<String, String > qualityMap = new HashMap<String, String>(){{
            put("99新","10.0");
            put("98新","9.8");
            put("95新","9.5");
            put("9新","9.0");
            put("85新","8.5");
            put("8新","8.0");
        }};
        // 将数据封装到同行价表里
        list.stream().forEach(phtTradePriceInfo -> {
            LuxPrice luxPrice = new LuxPrice();
            LuxSkuInfo luxSkuInfo = luxSkuInfoMapper.selectOne(new LambdaQueryWrapper<LuxSkuInfo>()
                    .eq(LuxSkuInfo::getSku, phtTradePriceInfo.getSku()));
            if(ObjectUtils.isEmpty(luxSkuInfo)){
                luxSkuInfo = new LuxSkuInfo();
                luxSkuInfo.setSku(phtTradePriceInfo.getSku());
                luxSkuInfo.setSeries(phtTradePriceInfo.getSeries());
                luxSkuInfo.setGrade(qualityMap.get(phtTradePriceInfo.getQualityValue()));
                luxSkuInfo.setBrandId(phtTradePriceInfo.getBrandId());
                luxSkuInfo.setBrand(phtTradePriceInfo.getBrandName());
                luxSkuInfo.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd")));
                luxSkuInfoMapper.insert(luxSkuInfo);
            }

            luxPrice.setSku(phtTradePriceInfo.getSku());
            luxPrice.setCrawlDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd")));
            Random random = new Random();
            Double rmbprice = 0.0;
            if(phtTradePriceInfo.getPrice() != null){
                rmbprice = phtTradePriceInfo.getPrice() * Double.valueOf(new DecimalFormat("#.0").format(random.nextDouble()*(1.5-1.3)+1.3));
            }
            luxPrice.setRmbPrice(rmbprice);
            luxPriceMapper.insert(luxPrice);
        });
    }
}
